import { request } from '@/api/request';
import { BASEAUTHTOKENFROMSBT, BASETOKENFROMSBT } from '@/common';
import { store } from '@/redux/store';

export type AssociationTreeDataDTO = {
  entId?: string;
  /** 查询类型 - 1: 备案人员; 2: 备案车辆 */
  type: number;
  /** 是否需要统计数量 - 默认 true */
  count: boolean;
  /**
   * NOTE: 需要区分sbt项目 还是安全教育项目
   *
   * 是否为安全教育平台 - 0: 否; 1: 是
   */
  isSafe?: number;
};

export type AssociationTreeDataVO = {
  entId: string;
  name: string;
  icon?: string;
  count: number;
};

/**
 * 备案企业
 *
 * @param params
 */
export async function getAssociationTreeData(params: AssociationTreeDataDTO) {
  const { data } = await request<{ list: AssociationTreeDataVO[]; total: number }>({
    method: 'get',
    url: '/code/api/association/listByTree',
    params: { ...params, isSafe: 1 },
    noAutoShowErrorMessage: true,
    headers: {
      ClientType: 'WebSafe',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
  });

  return {
    list: data.list,
    total: data.total,
  };
}
